package com.xc.controller;

import com.xc.common.Result;
import com.xc.common.SmsCLass;
import com.xc.entity.User;
import com.xc.service.DuanXinYanZheng1;
import com.xc.service.UserService;
import com.xc.utils.Randomutils;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/smartstudio/sms")
public class SMSSenderController {
    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    String token="";

    @GetMapping("/ver")
    public String smssender(@RequestParam(defaultValue = "")  String phone) throws Exception {

        User selectuser = userService.selectuser(phone);
        if(selectuser!=null&&selectuser.getDsUserTel().equals(phone))
        {
            String phoneNumber="+86"+phone;
            int random = Randomutils.Random();
            String verificationCode = "[\""+random+"\"]";
            String TemplateID="b24c53f62a55416496db382ceda37673";
            String passageway="8822033105187";
            ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
            String s = DuanXinYanZheng1.SMSsending(phoneNumber,verificationCode,TemplateID,passageway);
            if("Success".equals(s))
            {
                ops.set("login:code"+phone,random+"",2, TimeUnit.MINUTES);
                return s+"!"+random;
            }
            return "验证码发送失败";
        }
        return "用户不存在";
    }

    @GetMapping("/verification")
    public Result verification(@ApiParam(value = "电话号码", required = true)
                                    @RequestParam String  phone,
                                @ApiParam(value = "验证码", required = true)
                                    @RequestParam String verificationCode)
    {
        User selectuser = userService.selectuser(phone);
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String verificationCodeRedis = ops.get("login:code" + phone);
        token = UUID.randomUUID().toString();
        ops.set("token",token.substring(10,token.length()),5,TimeUnit.MINUTES);
        String tokenfront=token.substring(0,10);
        Result result=null;
        if(verificationCodeRedis.equals(verificationCode)&&selectuser.getDsUserTel().equals(phone))
        {
            System.out.println("成功");
            result=Result.succeed(new SmsCLass(selectuser.getDsUserName(),tokenfront));
        }
        else
        {
            result=Result.error("登录失败");
        }
        return result;
    }

    @GetMapping("/signInverificationCode")
    public Boolean signInverificationCode( @RequestParam String tokenfront)
    {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String tokenafter= ops.get("token");
        if((tokenfront+tokenafter).equals(token))
        {
            System.out.println("登录成功");
            return true;
        }
        return false;
    }

    @GetMapping("/notice/{phone}")
    public Boolean notice(@PathVariable String phone) throws Exception {
        String TemplateID="4d2ba2eeae4840c8a660e5e546ab4f66";
        String passageway="8822033030350";
        String phoneNumber="+86"+phone;
        String content="[\"12点20分\",\"工作室\",\"上号\"]";
        String s = DuanXinYanZheng1.SMSsending(phoneNumber,content,TemplateID,passageway);
        if("Success".equals(s))
        {
            System.out.println("成功");
            return true;
        }
        return false;
    }

}
